//
//  PersonalFunctionView.m
//  lcf-newproject
//
//  Created by yangyang on 14-11-24.
//  Copyright (c) 2014年 yang. All rights reserved.
//

#import "PersonalFunctionView.h"

@interface PersonalFunctionView ()

@property (nonatomic,copy)NSArray * functionIconArray;

@end

@implementation PersonalFunctionView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createPersonalFunctionView];
    }
    return self;
}

//创建个人功能九宫格
-(void)createPersonalFunctionView{


    _functionIconArray = @[@"jiao.png",@"tou.png",@"zhang.png",@"wo.png",@"yin.png",@"zi.png",@"zhai.png",@"xiang.png",@"shezhi.png",@"xiaoxi.png"];
    for (int i = 0; i<_functionIconArray.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setFrame:CGRectMake(PersonalButtonWidth(3)*(i%3), PersonalButtonHeight*(i/3), PersonalButtonWidth(3), PersonalButtonHeight)];
        [btn setBackgroundImage:[UIImage imageNamed:[_functionIconArray objectAtIndex:i]] forState:UIControlStateNormal];
        btn.tag = LCFPersonalTag + i;
        [self addSubview:btn];
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    for (int j = 0; j<(_functionIconArray.count/3)+1; j++) {
        UIImageView * horiImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"line22.png"]];
        horiImageView.alpha = 0.4;
        horiImageView.frame = CGRectMake(0, PersonalButtonHeight*j, self.bounds.size.width, 1) ;
        [self addSubview:horiImageView];
    }
    for (int k = 0; k<_functionIconArray.count/3; k++) {
        UIImageView * vertiImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"line21.png"]];
        vertiImageView.alpha = 0.4;
        vertiImageView.frame = CGRectMake(PersonalButtonWidth(3)*k, 0, 1,self.bounds.size.height );
        [self addSubview:vertiImageView];
        
    }
}

-(void)btnClick:(UIButton *)btn{
    for (UIView *view in self.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            UIButton * currentBtn = (UIButton *)view;
            if (currentBtn.tag == btn.tag) {
                currentBtn.selected = YES;
            }else{
                currentBtn.selected = NO;
            }
        }
    }
    NSInteger index = btn.tag-LCFPersonalTag;
    if ([self.delegate respondsToSelector:@selector(customTabBar:didSelectedIndex:)]) {
        [self.delegate customTabBar:self didSelectedIndex:index];
    }
}


@end
